package com.doctor.windflower_doctor.fragment;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.SystemApplication;
import com.doctor.windflower_doctor.util.StringUtil;
import com.lidroid.xutils.util.LogUtils;

public class MyQueationFragment extends BaseFragment implements OnClickListener {
	private FragmentManager fragmentManager;
	private Button left_btn;
	private View currentView;
	private BaseFragment currentFragment;
	private static MyQueationFragment myQueationFragment;
	private FragmentTransaction fragmentTransaction;
	private RelativeLayout search_layout, center_layout, right_layout;
	private EditText search_edittext;
	private String keyword;
	private ImageView center_red, right_red, delete_search;
	private int type;
	private PopupWindow pop_noty;
	private LinearLayout more_noty;

	public static MyQueationFragment getInstance(Bundle bundle) {
		if (myQueationFragment == null) {
			myQueationFragment = new MyQueationFragment();
			myQueationFragment.setArguments(bundle);
		}
		return myQueationFragment;
	}

	@Override
	protected View setConentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		fragmentManager = getChildFragmentManager();
		return inflater.inflate(R.layout.fragment_find_people, container, false);
	}

	@Override
	protected void getSaveData(Bundle savedInstanceState) {
	}

	@Override
	public void onStart() {
		LogUtils.e("=====================MyQueationFragment==========onStart==========");
		if (currentView == null) {
			currentView = left_btn;
		}
		onClick(currentView);
		super.onStart();
	}

	@Override
	protected void processLogic() {

	}

	@Override
	protected void setListener() {
		left_btn.setOnClickListener(this);
		center_layout.setOnClickListener(this);
		right_layout.setOnClickListener(this);
		delete_search.setOnClickListener(this);
		search_edittext.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
				if (arg1 == EditorInfo.IME_ACTION_SEARCH) {
					keyword = search_edittext.getText().toString().trim();
					if (StringUtil.isEmpty(keyword)) {
						Toast.makeText(getActivity(), "搜索内容不能为空", Toast.LENGTH_SHORT).show();
						return false;
					}
					// 先隐藏键盘
					((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getActivity().getCurrentFocus()
							.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
					QuestionClosedFragment closedFragment = (QuestionClosedFragment) currentFragment;
					closedFragment.post(keyword);
					return true;
				}
				return false;
			}
		});
		search_edittext.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				if (arg0.length() > 0) {
					delete_search.setVisibility(View.VISIBLE);
				} else {
					delete_search.setVisibility(View.GONE);
				}
			}
		});
	}

	@Override
	protected void findViews(View view) {
		left_btn = (Button) view.findViewById(R.id.left_btn);
		right_layout = (RelativeLayout) view.findViewById(R.id.right_layout);
		center_layout = (RelativeLayout) view.findViewById(R.id.center_layout);
		search_layout = (RelativeLayout) view.findViewById(R.id.search_layout);
		search_edittext = (EditText) view.findViewById(R.id.search_edittext);
		center_red = (ImageView) view.findViewById(R.id.center_red);
		right_red = (ImageView) view.findViewById(R.id.right_red);
		delete_search = (ImageView) view.findViewById(R.id.delete_search);
		if (type == 3) {
			currentView = right_layout;
		} else if (type == 2) {
			currentView = center_layout;
		}
	}

	

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.left_btn:
			search_layout.setVisibility(View.GONE);
			SystemApplication.getInstance().replyStatus = 1;
			changeFragment(QuestionToBeSolvedFragment.getInstance(new Bundle()));
			changeState(v);
			break;
		case R.id.center_layout:
			search_layout.setVisibility(View.GONE);
			SystemApplication.getInstance().replyStatus = 2;
			changeFragment(QuestionArrivingFragment.getInstance(new Bundle()));
			changeState(v);
			break;
		case R.id.right_layout:
			SystemApplication.getInstance().replyStatus = 3;
			search_layout.setVisibility(View.VISIBLE);
			changeFragment(QuestionClosedFragment.getInstance(new Bundle()));
			changeState(v);
			break;
		case R.id.delete_search:
			QuestionClosedFragment closedFragment = (QuestionClosedFragment) currentFragment;
			closedFragment.post("");
			delete_search.setVisibility(View.GONE);
			search_edittext.setText("");
			break;
		case R.id.more_noty:
			pop_noty.dismiss();
			break;
		}
	}

	public void changeState(View view) {
		left_btn.setSelected(false);
		center_layout.setSelected(false);
		right_layout.setSelected(false);
		view.setSelected(true);
		currentView = view;
	}

	public void changeFragment(BaseFragment fragment) {
		fragmentTransaction = fragmentManager.beginTransaction();
		if (currentFragment != null) {
			fragmentTransaction.hide(currentFragment);
		}
		if (!fragment.isAdded()) {
			fragmentTransaction.replace(R.id.question_list_layout, fragment);
		} else {
			fragmentTransaction.show(fragment);
		}
		currentFragment = fragment;
		fragmentTransaction.commit();
	}

	@Override
	public void onDestroy() {
		LogUtils.e("=====================MyQueationFragment==========onDestroy==========");
		super.onDestroy();
	}

	@Override
	public void onStop() {
		LogUtils.e("=====================MyQueationFragment==========onStop==========");
		super.onStop();
	}

	@Override
	public void onDestroyView() {
		currentView = null;
		LogUtils.e("=====================MyQueationFragment==========onDestroyView==========");
		super.onDestroyView();

	}

	@Override
	public void onPause() {
		LogUtils.e("=====================MyQueationFragment==========onPause==========");
		super.onPause();

	}

	@Override
	public void onResume() {
		LogUtils.e("=====================MyQueationFragment==========onResume==========");
		super.onResume();

	}

	public void postSelect(int type) {
		this.type = type;
	}

	public void postSelect1(int type) {
		onClick(center_layout);
	}
}
